/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FSterowanie.java
 *
 * Created on 2012-05-19, 03:36:15
 */
package oglTest;

import java.util.Timer;

/**
 *
 * @author Piotr
 */
public class FSterowanie extends javax.swing.JFrame {

    oglTest OGL;
    
    /** Creates new form FSterowanie */
    public FSterowanie() {
        initComponents();
    }
    
    public FSterowanie(oglTest OGL) {
        this.OGL = OGL;
        initComponents();
        
        // inicjalizacja elementow
        swatchslider.setMaximum(OGL.sonar.datagram68.size());
        swatchslider.setMinimum(0);
        swatchslider.setValue(OGL.sonar.akti); 
        swatchslider.setPaintTicks(true);
        swatchslider.setPaintLabels(true);
        swatchslider.setMajorTickSpacing(OGL.sonar.datagram68.size() / 5);
        swatchslider.setMinorTickSpacing(OGL.sonar.datagram68.size() / 50);
        
        jRadioButton2.setSelected(true); 
        
    }    

    public void updateSliderValue(){
        swatchslider.setValue(OGL.sonar.akti);
        currentFrameLabel.setText(""+OGL.sonar.akti);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        stepBackwardButton = new javax.swing.JButton();
        swatchslider = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        startBackwardButton = new javax.swing.JButton();
        stepForwardButton = new javax.swing.JButton();
        startForwardButton = new javax.swing.JButton();
        jumpTextField = new javax.swing.JTextField();
        jumpButton = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jRadioButton3 = new javax.swing.JRadioButton();
        scaleTextField = new javax.swing.JTextField();
        scaleButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        speedButton = new javax.swing.JButton();
        speedTextField = new javax.swing.JTextField();
        currentFrameLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Animation Settings");
        setAlwaysOnTop(true);
        setResizable(false);

        stepBackwardButton.setText("<");
        stepBackwardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stepBackwardButtonActionPerformed(evt);
            }
        });

        swatchslider.setToolTipText("");
        swatchslider.setValue(0);
        swatchslider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                swatchsliderStateChanged(evt);
            }
        });

        jLabel1.setText("Current frame:");

        startBackwardButton.setText("<< Start");
        startBackwardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startBackwardButtonActionPerformed(evt);
            }
        });

        stepForwardButton.setText(">");
        stepForwardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stepForwardButtonActionPerformed(evt);
            }
        });

        startForwardButton.setText("Start >>");
        startForwardButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startForwardButtonActionPerformed(evt);
            }
        });

        jumpTextField.setText("0");

        jumpButton.setText("Jump to frame");
        jumpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jumpButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("Follow terrain");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Follow sonar");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Water Column");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("Show only water column data");
        jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton3ActionPerformed(evt);
            }
        });

        scaleTextField.setText("0.5");

        scaleButton.setText("Sounding distance scale");
        scaleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scaleButtonActionPerformed(evt);
            }
        });

        stopButton.setText("Stop");
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });

        speedButton.setText("Animation speed");
        speedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                speedButtonActionPerformed(evt);
            }
        });

        speedTextField.setText("33");

        currentFrameLabel.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(currentFrameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(swatchslider, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jRadioButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jRadioButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                                .addComponent(jRadioButton3, javax.swing.GroupLayout.Alignment.LEADING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(speedButton, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                                .addComponent(jumpButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(scaleButton, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(speedTextField)
                                .addComponent(jumpTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)
                                .addComponent(scaleTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(stepBackwardButton)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(startBackwardButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(startForwardButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jCheckBox1))
                            .addGap(2, 2, 2)
                            .addComponent(stepForwardButton))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(currentFrameLabel))
                .addGap(1, 1, 1)
                .addComponent(swatchslider, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stepBackwardButton)
                    .addComponent(startBackwardButton)
                    .addComponent(startForwardButton)
                    .addComponent(stepForwardButton)
                    .addComponent(stopButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jumpButton)
                        .addComponent(jumpTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jRadioButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton2)
                    .addComponent(scaleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scaleButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton3)
                    .addComponent(speedTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(speedButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void swatchsliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_swatchsliderStateChanged
    OGL.sonar.akti = swatchslider.getValue();
    updateSliderValue();
    if (!OGL.animacja)
    {
        OGL.update = true;
    }
}//GEN-LAST:event_swatchsliderStateChanged

private void stepBackwardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stepBackwardButtonActionPerformed
    OGL.sonar.akti--;
    if (OGL.sonar.akti < 0) OGL.sonar.akti = 0;
    OGL.update = true;
    updateSliderValue();
}//GEN-LAST:event_stepBackwardButtonActionPerformed

private void startForwardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startForwardButtonActionPerformed
    OGL.animacja = true;
    OGL.kierunek = 2;
    OGL.wwd.repaint();
}//GEN-LAST:event_startForwardButtonActionPerformed

private void stepForwardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stepForwardButtonActionPerformed
    OGL.sonar.akti++;               
    if (OGL.sonar.akti >= OGL.sonar.datagram68.size()) OGL.sonar.akti = 0;
    OGL.update = true;
    updateSliderValue();
}//GEN-LAST:event_stepForwardButtonActionPerformed

    private void jumpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jumpButtonActionPerformed
         try {
              int klatka = Integer.parseInt(jumpTextField.getText());
                    if (klatka >= OGL.sonar.datagram68.size()) {
                        OGL.sonar.akti = OGL.sonar.datagram68.size()-1;
                        jumpTextField.setText(""+ OGL.sonar.akti);
                        swatchslider.setValue(klatka); 
                    }
                    else if (klatka < 0) { 
                        OGL.sonar.akti = 0;
                        jumpTextField.setText("0");
                        swatchslider.setValue(0); 
                    }
                    else{
                        OGL.sonar.akti = klatka;
                        swatchslider.setValue(klatka); 
                    }
                OGL.update = true;
                updateSliderValue();
	  } catch (Exception e) {
              jumpTextField.setText("0");
              swatchslider.setValue(0); 
	  }
       
    }//GEN-LAST:event_jumpButtonActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        boolean stan = jRadioButton1.isSelected();
        if(stan) {      
            OGL.czywc = false;
            OGL.wwd.repaint();
            
            OGL.zasonarem = false;
            OGL.update = true;
        }
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        boolean stan = jRadioButton2.isSelected();
        if(stan) {
            OGL.czywc = false;
            OGL.wwd.repaint();
            
            OGL.obx = OGL.pozycjax;      
            OGL.zasonarem = true;
            OGL.update = true;
        }
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        boolean stan = jCheckBox1.isSelected();
        if(stan) {
            OGL.sonar.jestawc = true;  
            OGL.update = true;
        }
        else {
            OGL.sonar.jestawc = false; 
            OGL.update = true;
        }
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton3ActionPerformed
        boolean stan = jRadioButton3.isSelected();
        if(stan) {
            OGL.czywc = true;
            OGL.wwd.repaint();
            OGL.update = true;
        }
        else {
            OGL.czywc = false;
            OGL.wwd.repaint();
            OGL.update = true;
        }
    }//GEN-LAST:event_jRadioButton3ActionPerformed

    private void scaleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scaleButtonActionPerformed
        try {
              float odstep = Float.parseFloat(scaleTextField.getText());
                    if (odstep >= 5.0f) {
                        OGL.odstep = 5.0f;
                        scaleTextField.setText(""+ OGL.odstep);
                    }
                    else if (odstep < 0.1) {
                        OGL.odstep = 0.1f;
                        scaleTextField.setText("0.1");
                    }
                    else{
                        OGL.odstep = odstep; 
                    }
                OGL.wwd.repaint();    
                OGL.update = true;
	  } catch (Exception e) {
              jumpTextField.setText("0.5f");
	  }
    }//GEN-LAST:event_scaleButtonActionPerformed

private void startBackwardButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startBackwardButtonActionPerformed
    OGL.animacja = true;
    OGL.kierunek = -2;
    OGL.wwd.repaint();
}//GEN-LAST:event_startBackwardButtonActionPerformed

private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
    OGL.animacja = false;
}//GEN-LAST:event_stopButtonActionPerformed

private void speedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_speedButtonActionPerformed
    int czas = Integer.parseInt(speedTextField.getText());
    if(czas > 1000) czas = 1000;
    if(czas < 10) czas = 10;
    OGL.nowyCzas(czas);
}//GEN-LAST:event_speedButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel currentFrameLabel;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JButton jumpButton;
    private javax.swing.JTextField jumpTextField;
    private javax.swing.JButton scaleButton;
    private javax.swing.JTextField scaleTextField;
    private javax.swing.JButton speedButton;
    private javax.swing.JTextField speedTextField;
    private javax.swing.JButton startBackwardButton;
    private javax.swing.JButton startForwardButton;
    private javax.swing.JButton stepBackwardButton;
    private javax.swing.JButton stepForwardButton;
    private javax.swing.JButton stopButton;
    private javax.swing.JSlider swatchslider;
    // End of variables declaration//GEN-END:variables
}
